/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jmenu.gui;

import javax.swing.JTabbedPane;
import jmenu.dbmodels.MSectionsOfFood;
import jmenu.dbmodels.MSubsectionsOfFood;
import jmenu.main.Config;
import jmenu.main.Icons;

/**
 *
 * @author sevcik.roman
 */
public class FoodPanelThread extends Thread
{
    private StatusPanel panel;
    private JTabbedPane pane;

    public FoodPanelThread(StatusPanel panel,JTabbedPane pane)
    {
        super("StatusPanelThread");
        this.panel = panel;
        this.pane = pane;
    }

    
    @Override
    public void run()
    {
        this.panel.setStatusMessage("Loading data from database...", Icons.STATUS_INFO);
        this.panel.setTaskRunning(true);
        this.panel.setProgress(0, "", false, true);

        MSectionsOfFood sections = Config.getKernel().queries.loadAllSectionOfFoodOrderBySectionId();
        MSubsectionsOfFood subsection;
        JTabbedPane subsectionTabbedPane;

        this.pane.add("Hledat", new FoodPanel() );

        for (int j=0; j < sections.getRows().size(); j++)
        {
            subsection = Config.getKernel().queries.loadAllSubsectionOfFoodBySectionIdOrderBySubsectionId(sections.getRow(j).getId());
            subsectionTabbedPane = new JTabbedPane(JTabbedPane.BOTTOM);

            FoodPanel fpanel;

            int size = subsection.getRows().size();

            for (int k=0; k < size; k++)
            {
                fpanel = new FoodPanel(Config.getKernel().queries.loadAllFoodsBySubsectionID(subsection.getRow(k).getId()));
                subsectionTabbedPane.add(subsection.getRow(k).getName(), fpanel);
            }
            this.pane.add(sections.getRow(j).getName(), subsectionTabbedPane);

            this.panel.setProgress(Math.round(100*j/sections.getRows().size()), "", false, true);
        }


        this.pane.setVisible(true);

        this.panel.setStatusMessage(new String("Loading data from database...Done"), Icons.STATUS_INFO);
        this.panel.setTaskRunning(false);
        this.panel.setProgress(0, "", false, false);
        this.panel.hideStatusMessageAfter(5000);
    }
}
